import { post, get } from '../utils/request'
import axios from 'axios'

// API响应类型
interface ApiResponse<T = any> {
  code: number
  msg: string
  data: T
}

// 上传图片返回数据类型 - 更新为返回ID
export interface UploadImageResponse {
  id: number // 更新为ID
}

// 回显图片参数类型 - 更新为使用imageId
export interface FeedbackImageParams {
  imageId: number // 更新为使用imageId
}

// 上传蒙版参数类型 - 更新为使用originalId
export interface UploadMaskParams {
  originalId: string // 更新为使用originalId
}

// Lore文生图接口
export interface LorewstRequest {
  prompt: string;    // 关键词
  loreName: string;  // 模型名称
}

export interface LorewstResponse {
  promptId: string;
  viewUrls: string[];
}

// 局部重绘（蒙版）接口
export interface JbchRequest {
  majorId: number;    // 主图ID
  minorId: number;    // 附图ID
  prompt: string;     // 关键词
  isMask: number;     // 图片蒙版状态(0:不蒙版,1:主图蒙版,2:二图蒙版)
}

export interface JbchResponse {
  promptId: string;
  viewUrls: string[];
}

// 图像融合接口
export interface StrhzxsRequest {
  loreName: string;
  majorId: number;
  minorId: number;
  majorStrength: number;
  minorStrength: number;
  structuralStrength: number;
}

// 图加图无锁接口
export interface TjtwsRequest {
  loreName: string;  // 模型名称
  majorId: number;   // 主图ID
  minorId: number;   // 附图ID
  majorStrength: number;  // 主图强度
  minorStrength: number;  // 附图强度
}

export interface StrhzxsResponse {
  promptId: string;
  viewUrls: string[];
}

// 鞋底互换接口
export interface XdhhRequest {
  majorId: number;    // 鞋面图ID
  minorId: number;    // 鞋底图ID
  maskStates: number; // 图片蒙版状态(0:不蒙版,1:蒙版鞋面图,2:蒙版鞋底图,3:蒙版两图)
}

export interface XdhhResponse {
  promptId: string;
  viewUrls: string[];
}

// 图加图OK接口
export interface TstokRequest {
  imageId: number;   // 主图ID
  loreName: string;  // 模型名称
  denoise: number;   // 噪点强度(0-10)
}

export interface TstokResponse {
  promptId: string;
  viewUrls: string[];
}

// 高清放大接口
export interface GqfdRequest {
  imageId: number; // 主图ID
}

export interface GqfdResponse {
  promptId: string;
  viewUrls: string[];
}

// 元素消除接口
export interface XcRequest {
  imageId: number;   // 主图ID
  isMask: number;    // 是否是蒙版 (1表示是蒙版)
}

export interface XcResponse {
  promptId: string;
  viewUrls: string[];
}

// 抠图接口
export interface KtRequest {
  imageId: number;   // 图像ID
}

export interface KtResponse {
  promptId: string;
  viewUrls: string[];
}

// 修复（蒙版）接口
export interface XfRequest {
  imageId: number;   // 主图ID
  isMask: number;    // 是否是蒙版 (1表示是蒙版)
}

export interface XfResponse {
  promptId: string;
  viewUrls: string[];
}

/**
 * 检查用户是否已登录
 * @returns boolean 是否已登录
 */
export function isUserLoggedIn(): boolean {
  const token = localStorage.getItem('token')
  return !!token
}

/**
 * 获取当前token
 * @returns string|null 当前token
 */
export function getCurrentToken(): string | null {
  return localStorage.getItem('token')
}

/**
 * 上传图片
 * @param file 要上传的文件
 * @returns Promise 返回上传结果
 */
export function uploadImage(file: File) {
  const formData = new FormData()
  formData.append('image', file)
  
  const token = localStorage.getItem('token')
  const bearerToken = token?.startsWith('Bearer ') ? token : `Bearer ${token}`
  
  // 更新为新的API路径
  return axios.post('/api/oss/upload', formData, {
    headers: {
      'Authorization': bearerToken,
      'token': bearerToken,
      'Content-Type': 'multipart/form-data',
    }
  }).then(response => {
    console.log("上传响应:", response)
    return response.data
  }).catch(error => {
    console.error("上传错误:", error)
    console.error("错误响应:", error.response?.data)
    throw error
  })
}

/**
 * 回显图片
 * @param imageId 图片ID或包含ID的对象
 * @returns Promise 返回图片URL
 */
export function feedbackImage(imageId: number | { id: number }) {
  // 确保imageId是一个数字
  const id = typeof imageId === 'object' ? imageId.id : imageId;
  
  // 使用axios直接请求图片，设置responseType为blob以正确处理二进制数据
  const token = localStorage.getItem('token')
  const bearerToken = token?.startsWith('Bearer ') ? token : `Bearer ${token}`
  
  return axios.get(`/api/oss/view?imageId=${id}`, {
    headers: {
      'Authorization': bearerToken,
      'token': bearerToken
    },
    responseType: 'blob'
  }).then(response => {
    // 创建一个blob URL
    const blob = new Blob([response.data], { type: response.headers['content-type'] || 'image/png' })
    const url = URL.createObjectURL(blob)
    return { code: 0, msg: "success", data: url }
  }).catch(error => {
    console.error("图片获取错误:", error)
    return { code: 500, msg: error.message, data: "" }
  })
}

/**
 * 直接构建完整的图片URL
 * @param imageId 图片ID或包含ID的对象
 * @returns 图片的Blob URL
 */
export function buildImageUrl(imageId: number | { id: number }): Promise<string> {
  // 确保imageId是一个数字
  const id = typeof imageId === 'object' ? imageId.id : imageId;
  
  // 获取token
  const token = localStorage.getItem('token')
  const bearerToken = token?.startsWith('Bearer ') ? token : `Bearer ${token}`
  
  // 返回Promise以异步获取blob URL
  return new Promise((resolve, reject) => {
    axios.get(`/api/oss/view?imageId=${id}`, {
      headers: {
        'Authorization': bearerToken,
        'token': bearerToken
      },
      responseType: 'blob'
    }).then(response => {
      // 创建一个blob URL
      const blob = new Blob([response.data], { type: response.headers['content-type'] || 'image/png' })
      const url = URL.createObjectURL(blob)
      resolve(url)
    }).catch(error => {
      console.error("构建图片URL失败:", error)
      reject(error)
    })
  })
}

// 处理URL辅助函数 - 提取出来重用
const processResponseUrls = async (responseData: any) => {
  // 优先处理错误码1013
  if (responseData.code === 1013) {
    // 替换错误消息
    responseData.msg = "请先选择需要更改的区域！";
    return responseData;
  }
  
  if (responseData.code === 0 || responseData.code === 200) {
    // 确定viewUrls的位置
    let viewUrls = responseData.data?.viewUrls || responseData.viewUrls;
    
    if (viewUrls && Array.isArray(viewUrls)) {
      // 创建一个新数组来存储处理后的URL
      const processedUrls = [];
      
      // 处理每个URL
      for (let i = 0; i < viewUrls.length; i++) {
        const url = viewUrls[i];
        try {
          // 如果URL已经是完整的HTTP URL，直接使用
          if (url.startsWith('http')) {
            processedUrls.push(url);
          } else {
            // 否则，使用buildImageUrl获取Blob URL
            const imageId = url.split('?imageId=')[1] || url;
            try {
              const blobUrl = await buildImageUrl(parseInt(imageId, 10));
              processedUrls.push(blobUrl);
            } catch (error) {
              console.error('转换为blob URL失败:', error);
              processedUrls.push(url); // 回退到原始URL
            }
          }
        } catch (error) {
          console.error('URL处理失败:', error);
          processedUrls.push(url); // 回退到原始URL
        }
      }
      
      // 更新响应中的viewUrls
      if (responseData.data?.viewUrls) {
        responseData.data.viewUrls = processedUrls;
      } else if (responseData.viewUrls) {
        responseData.viewUrls = processedUrls;
      }
    }
  }
  
  return responseData;
};

// 图像融合API
export function strhzxs(data: StrhzxsRequest) {
  const token = localStorage.getItem('token')
  const bearerToken = token?.startsWith('Bearer ') ? token : `Bearer ${token}`
  
  return axios.post('/api/image/gene/strhzxs', data, {
    headers: {
      'Authorization': bearerToken,
      'Content-Type': 'application/json'
    }
  }).then(async response => {
    // 处理响应数据
    return await processResponseUrls(response.data);
  })
}

/**
 * 图加图无锁 API
 * 
 * 接口地址: /api/image/gene/tjtws
 * 
 * @param data TjtwsRequest 请求参数
 * @returns Promise 返回生成结果
 */
export function tjtws(data: TjtwsRequest) {
  const token = localStorage.getItem('token')
  const bearerToken = token?.startsWith('Bearer ') ? token : `Bearer ${token}`
  
  return axios.post('/api/image/gene/tjtws', data, {
    headers: {
      'Authorization': bearerToken,
      'Content-Type': 'application/json'
    }
  }).then(async response => {
    // 处理响应数据
    console.log('图加图无锁响应:', response.data)
    return await processResponseUrls(response.data);
  }).catch(error => {
    console.error('图加图无锁错误:', error)
    console.error('错误响应:', error.response?.data)
    throw error
  })
}

/**
 * 鞋底互换（蒙版）API
 * 
 * 接口地址: /api/image/gene/xdhh
 * 
 * @param data XdhhRequest 请求参数
 * @returns Promise 返回生成结果
 */
export function xdhh(data: XdhhRequest) {
  // 获取token
  const token = localStorage.getItem('token')
  const bearerToken = token?.startsWith('Bearer ') ? token : `Bearer ${token}`
  
  // 添加认证头
  return axios.post('/api/image/gene/xdhh', data, {
    headers: {
      'Authorization': bearerToken,
      'Content-Type': 'application/json'
    }
  }).then(async response => {
    // 处理响应数据
    console.log('鞋底互换响应:', response.data)
    return await processResponseUrls(response.data);
  }).catch(error => {
    console.error('鞋底互换错误:', error)
    console.error('错误响应:', error.response?.data)
    throw error
  });
}

// 高清放大API
export function gqfd(data: GqfdRequest) {
  // 获取token
  const token = localStorage.getItem('token')
  const bearerToken = token?.startsWith('Bearer ') ? token : `Bearer ${token}`
  
  // 添加认证头
  return axios.post('/api/image/gene/gqfd', data, {
    headers: {
      'Authorization': bearerToken,
      'Content-Type': 'application/json'
    }
  }).then(async response => {
    // 处理响应数据
    console.log('高清放大响应:', response.data)
    return await processResponseUrls(response.data);
  }).catch(error => {
    console.error('高清放大错误:', error)
    console.error('错误响应:', error.response?.data)
    throw error
  });
}

// 元素消除API
export function xc(data: XcRequest) {
  // 获取token
  const token = localStorage.getItem('token')
  const bearerToken = token?.startsWith('Bearer ') ? token : `Bearer ${token}`
  
  // 添加认证头
  return axios.post('/api/image/gene/xc', data, {
    headers: {
      'Authorization': bearerToken,
      'Content-Type': 'application/json'
    }
  }).then(async response => {
    // 处理响应数据
    console.log('元素消除响应:', response.data)
    return await processResponseUrls(response.data);
  }).catch(error => {
    console.error('元素消除错误:', error)
    console.error('错误响应:', error.response?.data)
    throw error
  });
}

// 抠图API
export function kt(data: KtRequest) {
  // 获取token
  const token = localStorage.getItem('token')
  const bearerToken = token?.startsWith('Bearer ') ? token : `Bearer ${token}`
  
  // 添加认证头
  return axios.post('/api/image/gene/kt', data, {
    headers: {
      'Authorization': bearerToken,
      'Content-Type': 'application/json'
    }
  }).then(async response => {
    // 处理响应数据
    console.log('抠图响应:', response.data)
    return await processResponseUrls(response.data);
  }).catch(error => {
    console.error('抠图错误:', error)
    console.error('错误响应:', error.response?.data)
    throw error
  });
}

/**
 * 上传蒙版
 * @param file 蒙版文件
 * @param originalId 原始图片ID
 * @returns Promise 返回上传结果
 */
export function uploadMask(file: File, originalId: string) {
  const formData = new FormData()
  formData.append('image', file)
  formData.append('originalId', String(originalId))
  // formData.append('originalId', String(Number(originalId)+1))
  
  const token = localStorage.getItem('token')
  const bearerToken = token?.startsWith('Bearer ') ? token : `Bearer ${token}`
  
  // 更新为新的API路径和参数
  return axios.post(`/api/oss/mask`, formData, {
    headers: {
      'Authorization': bearerToken,
      'token': bearerToken,
      'Content-Type': 'multipart/form-data',
    }
  }).then(response => {
    console.log("上传蒙版响应:", response)
    return response.data
  }).catch(error => {
    console.error("上传蒙版错误:", error)
    console.error("错误响应:", error.response?.data)
    throw error
  })
}

/**
 * 图加图OK API
 * 
 * 接口地址: /api/image/gene/tstok
 * 
 * @param data TstokRequest 请求参数
 * @returns Promise 返回生成结果
 */
export function tstok(data: TstokRequest) {
  const token = localStorage.getItem('token')
  const bearerToken = token?.startsWith('Bearer ') ? token : `Bearer ${token}`
  
  return axios.post('/api/image/gene/tstok', data, {
    headers: {
      'Authorization': bearerToken,
      'Content-Type': 'application/json'
    }
  }).then(async response => {
    // 处理响应数据
    console.log('图加图OK响应:', response.data)
    return await processResponseUrls(response.data);
  }).catch(error => {
    console.error('图加图OK错误:', error)
    console.error('错误响应:', error.response?.data)
    throw error
  })
}

/**
 * 局部重绘（蒙版）API
 * 
 * 接口地址: /api/image/gene/jbch
 * 
 * @param data JbchRequest 请求参数
 * @returns Promise 返回生成结果
 */
export function jbch(data: JbchRequest) {
  const token = localStorage.getItem('token')
  const bearerToken = token?.startsWith('Bearer ') ? token : `Bearer ${token}`
  
  return axios.post('/api/image/gene/jbch', data, {
    headers: {
      'Authorization': bearerToken,
      'Content-Type': 'application/json'
    }
  }).then(async response => {
    // 处理响应数据
    console.log('局部重绘响应:', response.data)
    return await processResponseUrls(response.data);
  }).catch(error => {
    console.error('局部重绘错误:', error)
    console.error('错误响应:', error.response?.data)
    throw error
  })
}

/**
 * Lore文生图API
 * 
 * 接口地址: /api/image/gene/lorewst
 * 
 * @param data LorewstRequest 请求参数
 * @returns Promise 返回生成结果
 */
export function lorewst(data: LorewstRequest) {
  const token = localStorage.getItem('token')
  const bearerToken = token?.startsWith('Bearer ') ? token : `Bearer ${token}`
  
  return axios.post('/api/image/gene/lorewst', data, {
    headers: {
      'Authorization': bearerToken,
      'Content-Type': 'application/json'
    }
  }).then(async response => {
    // 处理响应数据
    console.log('Lore文生图响应:', response.data)
    return await processResponseUrls(response.data);
  }).catch(error => {
    console.error('Lore文生图错误:', error)
    console.error('错误响应:', error.response?.data)
    throw error
  })
}

/**
 * 修复（蒙版）API
 * 
 * 接口地址: /api/image/gene/xf
 * 
 * @param data XfRequest 请求参数
 * @returns Promise 返回生成结果
 */
export function xf(data: XfRequest) {
  const token = localStorage.getItem('token')
  const bearerToken = token?.startsWith('Bearer ') ? token : `Bearer ${token}`
  
  return axios.post('/api/image/gene/xf', data, {
    headers: {
      'Authorization': bearerToken,
      'Content-Type': 'application/json'
    }
  }).then(async response => {
    // 处理响应数据
    console.log('图片修复响应:', response.data)
    return await processResponseUrls(response.data);
  }).catch(error => {
    console.error('图片修复错误:', error)
    console.error('错误响应:', error.response?.data)
    throw error
  })
 } 